package facilius.controller.action.frequencia;

import facilius.model.ServiceLocator;
import facilius.model.pojo.Frequencia;
import facilius.model.pojo.UsuarioCursoTurma;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.mentawai.core.BaseAction;

/**
 *
 * @author Graciano
 */
public class FrequenciaCreateAction extends BaseAction {

    @Override
    public String execute() throws Exception {
        String consequence = ERROR;
        Map<String, Object> presentes = new HashMap<String, Object>();
        try {
            String presencas[] = input.getStrings("presenca");
            String aulas[] = input.getStrings("aulaId");
            List<UsuarioCursoTurma> users = null;

            if (presencas != null) {

                for (int i = 0; i < presencas.length; i++) {
                    String matricula = presencas[i];

                    Map<String, Object> criteriaUsuarioCurso = new HashMap<String, Object>();
                    criteriaUsuarioCurso.put("aulaId", Long.parseLong(aulas[i]));
                    users = ServiceLocator.getUsuarioCursoTurmaService().readByCriteria(criteriaUsuarioCurso);

                    int j = 0;
                    for (j = 0; j < users.size(); j++) {
                        if (users.get(j).getUsuarioCurso().getMatricula() == Long.parseLong(matricula)) {
                            Frequencia f = new Frequencia();
                            f.setAula(ServiceLocator.getAulaService().readById(Long.parseLong(aulas[i])));
                            f.setUsuarioCurso(users.get(j).getUsuarioCurso());
                            f.setStatus(true);

                            ServiceLocator.getFrequenciaService().create(f);

                            presentes.put(users.get(j).getUsuarioCurso().getMatricula().toString(), users.get(j));
                            break;
                        }
                    }
                }
            }
            consequence = SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return consequence;
    }
}
